package com.gca.airhockey;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

public class Puck implements ContactListener, IPhysicalObject {
	private static final String TABLE_IMAGE = "gfx/puck.png";

	private static final int POS_X = 650;
	private static final int POS_Y = 376;

	private BitmapTextureAtlas mTexture;
	private TextureRegion mRegion;
	private Body mBody;
	private Sprite mSprite;

	// ===========================================================
	// Constructors
	// ===========================================================
	public Puck(Context pContext, Engine engine,PhysicsWorld physicsWorld) {
		
		SoundManager.addSound(1, R.raw.puck_hitting_pallet);
		SoundManager.addSound(2, R.raw.puck_hitting_side);
		SoundManager.addSound(3, R.raw.puck_goal);
		
		physicsWorld.setContactListener(this);

		this.mTexture = new BitmapTextureAtlas(128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mTexture, pContext, TABLE_IMAGE, 0, 0);
		engine.getTextureManager().loadTexture(this.mTexture);
		this.mSprite = new Sprite(POS_X, POS_Y, mRegion);

		this.mSprite.setScale(.5f);
		
		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.7f, 0);
		
		
		mBody = PhysicsFactory.createCircleBody(physicsWorld, mSprite, BodyType.DynamicBody, objectFixtureDef);
		mBody.setFixedRotation(true);
		
		physicsWorld.registerPhysicsConnector(new PhysicsConnector(mSprite, mBody, true, true));
		mBody.setUserData(this);
			
	}

	// ===========================================================
	// Getters and Setters
	// ===========================================================
	public Sprite getSprite() {
		return mSprite;
	} 
	public Body getBody() {                                                                                                                                                                                
		return mBody;
	} 
 
	@Override
	public void beginContact(Contact contact) {
		IPhysicalObject pObject = (IPhysicalObject)contact.getFixtureA().getBody().getUserData();
		String sType = contact.getFixtureA().getBody().getUserData().toString();
		if (sType == "Puck")
		{
			 pObject = (IPhysicalObject)contact.getFixtureB().getBody().getUserData();
			sType = contact.getFixtureB().getBody().getUserData().toString();
		}
			
		
		Log.v("App",sType);
			
		if (sType == "Paddle") {
			SoundManager.playSound(1,1);
		}
		else if (sType == "Goal") {
			SoundManager.playSound(3,1);
		} else {
			SoundManager.playSound(2,1);
		}
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub 
		IPhysicalObject pObject = (IPhysicalObject)contact.getFixtureA().getBody().getUserData();
		String sType = contact.getFixtureA().getBody().getUserData().toString();
		if (sType == "Puck")
		{
			 pObject = (IPhysicalObject)contact.getFixtureB().getBody().getUserData();
			sType = contact.getFixtureB().getBody().getUserData().toString();
		}
		if (sType == "Paddle") {
			if (pObject != null)
			{
							Paddle p = (Paddle)pObject;
			float xDeplacement = p.getXDeplacement();
			float yDeplacement = p.getYDeplacement();
			float vitesse = Math.max(1,(float) Math.sqrt(xDeplacement*xDeplacement+yDeplacement*yDeplacement));
			
			//this.mBody.setLinearVelocity(xDeplacement, yDeplacement);
			
			//this.mBody.applyForce(xDeplacement, yDeplacement, 0, 0);
			Vector2 v = this.mBody.getLinearVelocity().cpy();
			v.x = v.x + vitesse;
			v.y = v.y + vitesse;
			 
			this.mBody.applyForce(v.x, v.y, this.mSprite.getWidthScaled()/2, this.mSprite.getHeightScaled()/2);
			//this.mBody.setLinearVelocity(v);	
			}
		}
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public String toString() {
		return "Puck";
	}
}
